<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="钱包账户号" prop="walletNo">
        <el-input
          v-model="queryParams.walletNo"
          placeholder="请输入钱包账户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户号" prop="custNo">
        <el-input
          v-model="queryParams.custNo"
          placeholder="请输入客户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="变更类型" prop="changeType">
        <el-select v-model="queryParams.changeType" placeholder="请选择变更类型" clearable size="small">
          <el-option
            v-for="dict in changeTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资金类型" prop="fundsType">
        <el-select v-model="queryParams.fundsType" placeholder="请选择资金类型" clearable size="small">
          <el-option
            v-for="dict in fundsTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="钱包类型" prop="walletType">
        <el-select v-model="queryParams.walletType" placeholder="请选择钱包类型" clearable size="small">
          <el-option label="请选择字典生成" value=""/>
        </el-select>
      </el-form-item>
      <el-form-item label="变更金额" prop="changeAmount">
        <el-input
          v-model="queryParams.changeAmount"
          placeholder="请输入变更金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="From" prop="fromWhere">
        <el-input
          v-model="queryParams.fromWhere"
          placeholder="请输入From"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="To" prop="toDest">
        <el-input
          v-model="queryParams.toDest"
          placeholder="请输入To"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="钱包订单" prop="orderSerialNo">
        <el-input
          v-model="queryParams.orderSerialNo"
          placeholder="请输入钱包订单"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联借据" prop="loanSerialNo">
        <el-input
          v-model="queryParams.loanSerialNo"
          placeholder="请输入关联借据"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三方交易流水号" prop="reference">
        <el-input
          v-model="queryParams.reference"
          placeholder="请输入三方交易流水号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="create date">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="start time"
          end-placeholder="End time"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wallet:wallet_flow:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wallet:wallet_flow:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wallet:wallet_flow:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wallet:wallet_flow:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wallet_flowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="钱包账户号" align="center" prop="walletNo" width="280"/>
      <el-table-column label="客户号" align="center" prop="custNo" width="200"/>
      <el-table-column label="变更类型" align="center" prop="changeType" width="120" :formatter="changeTypeFormat"/>
      <el-table-column label="资金类型" align="center" prop="fundsType" width="160" :formatter="fundsTypeFormat"/>
      <el-table-column label="钱包类型" align="center" prop="walletType" width="120"/>
      <el-table-column label="变更金额" align="center" prop="changeAmount" width="150"/>
      <el-table-column label="From" align="center" prop="fromWhere" width="260"/>
      <el-table-column label="To" align="center" prop="toDest" width="260"/>
      <el-table-column label="钱包订单" align="center" prop="orderSerialNo" width="280"/>
      <el-table-column label="关联借据" align="center" prop="loanSerialNo" width="260"/>
      <el-table-column label="三方交易流水号" align="center" prop="reference" width="260"/>
      <el-table-column label="支付时间" align="center" prop="pay_time" width="180"/>
      <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wallet:wallet_flow:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wallet:wallet_flow:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改钱包流水对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="钱包账户号" prop="walletNo">
          <el-input v-model="form.walletNo" placeholder="请输入钱包账户号"/>
        </el-form-item>
        <el-form-item label="客户号" prop="custNo">
          <el-input v-model="form.custNo" placeholder="请输入客户号"/>
        </el-form-item>
        <el-form-item label="变更类型" prop="changeType">
          <el-select v-model="form.changeType" placeholder="请选择变更类型">
            <el-option
              v-for="dict in changeTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资金类型" prop="fundsType">
          <el-select v-model="form.fundsType" placeholder="请选择资金类型">
            <el-option
              v-for="dict in fundsTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="钱包类型" prop="walletType">
          <el-select v-model="form.walletType" placeholder="请选择钱包类型">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="变更金额" prop="changeAmount">
          <el-input v-model="form.changeAmount" placeholder="请输入变更金额"/>
        </el-form-item>
        <el-form-item label="From" prop="fromWhere">
          <el-input v-model="form.fromWhere" placeholder="请输入From"/>
        </el-form-item>
        <el-form-item label="To" prop="toDest">
          <el-input v-model="form.toDest" placeholder="请输入To"/>
        </el-form-item>
        <el-form-item label="钱包订单" prop="orderSerialNo">
          <el-input v-model="form.orderSerialNo" placeholder="请输入钱包订单"/>
        </el-form-item>
        <el-form-item label="关联借据" prop="loanSerialNo">
          <el-input v-model="form.loanSerialNo" placeholder="请输入关联借据"/>
        </el-form-item>
        <el-form-item label="三方交易流水号" prop="reference">
          <el-input v-model="form.reference" placeholder="请输入三方交易流水号"/>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option
              v-for="dict in stateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listWallet_flow, getWallet_flow, delWallet_flow, addWallet_flow, updateWallet_flow, exportWallet_flow} from "@/api/wallet/wallet_flow";

export default {
  name: "Wallet_flow",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 钱包流水表格数据
      wallet_flowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 变更类型字典
      changeTypeOptions: [],
      // 资金类型字典
      fundsTypeOptions: [],
      // 状态字典
      stateOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        walletNo: null,
        custNo: null,
        changeType: null,
        fundsType: null,
        walletType: null,
        changeAmount: null,
        fromWhere: null,
        toDest: null,
        orderSerialNo: null,
        loanSerialNo: null,
        reference: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        walletNo: [
          {required: true, message: "钱包账户号不能为空", trigger: "blur"}
        ],
        custNo: [
          {required: true, message: "客户号不能为空", trigger: "blur"}
        ],
        changeType: [
          {required: true, message: "变更类型不能为空", trigger: "change"}
        ],
        fundsType: [
          {required: true, message: "资金类型不能为空", trigger: "change"}
        ],
        walletType: [
          {required: true, message: "钱包类型不能为空", trigger: "change"}
        ],
        changeAmount: [
          {required: true, message: "变更金额不能为空", trigger: "blur"}
        ],
        state: [
          {required: true, message: "状态不能为空", trigger: "change"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("change_type").then(response => {
      this.changeTypeOptions = response.data;
    });
    this.getDicts("funds_type").then(response => {
      this.fundsTypeOptions = response.data;
    });
    this.getDicts("state").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    /** 查询钱包流水列表 */
    getList() {
      this.loading = true;
      listWallet_flow(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.wallet_flowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 变更类型字典翻译
    changeTypeFormat(row, column) {
      return this.selectDictLabel(this.changeTypeOptions, row.changeType);
    },
    // 资金类型字典翻译
    fundsTypeFormat(row, column) {
      return this.selectDictLabel(this.fundsTypeOptions, row.fundsType);
    },
    // 状态字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        walletNo: null,
        custNo: null,
        changeType: null,
        fundsType: null,
        walletType: null,
        changeAmount: null,
        fromWhere: null,
        toDest: null,
        orderSerialNo: null,
        loanSerialNo: null,
        reference: null,
        state: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        dateRange: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.dateRange = [];
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加钱包流水";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWallet_flow(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改钱包流水";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWallet_flow(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWallet_flow(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除钱包流水编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delWallet_flow(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有钱包流水数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportWallet_flow(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
